package com.fmy.jurisdiction.modular.controller;


import com.fmy.jurisdiction.core.commons.kits.SecurityKit;
import com.fmy.jurisdiction.core.commons.status.PermissionType;
import com.fmy.jurisdiction.core.web.call.ResponseCall;
import com.fmy.jurisdiction.core.web.specs.Response;
import com.fmy.jurisdiction.core.web.util.WebUtil;
import com.fmy.jurisdiction.modular.model.AdminPermission;
import com.fmy.jurisdiction.modular.service.IAdminPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 权限表  前端控制器
 * </p>
 *
 * @author Mr.fmy
 * @since 2019-09-16
 */
@RestController
@RequestMapping("/admin-permission")
public class AdminPermissionController {

    @Autowired
    private IAdminPermissionService permissionService;

    @GetMapping("/menus")
    public Response getMenus() {
        return ResponseCall.success(permissionService.getChilds(SecurityKit.getId(), PermissionType.MENU_TYPE));
    }

    @GetMapping("/list")
    public Response list() {
        return ResponseCall.success(permissionService.getChilds(null, null));
    }

    /**
     * 获取所属权限Code
     */
    @GetMapping("/get-code")
    public Response getCode() {
        return ResponseCall.success(permissionService.getPermCode(SecurityKit.getId()));
    }

    @PostMapping("/add")
    @Valid
    public Response add(AdminPermission permission, BindingResult result) {
        WebUtil.validationParam(result);
        permission.setCreateUser(SecurityKit.getId());
        return WebUtil.vailBusiness(permissionService.add(permission), "新增权限失败");
    }

    @PostMapping("/modify")
    public Response modify(@RequestParam(required = false) Integer id, String name, String url, String icon, PermissionType type) {
        return WebUtil.vailBusiness(permissionService.modifyPerm(id, name, url, icon, type, SecurityKit.getId()), "修改失败、请稍后重试");
    }

    @PostMapping("/del")
    public Response del(@RequestParam(required = false) Integer id) {
        return WebUtil.vailBusiness(permissionService.delPerm(id), "删除失败、请稍后重试");
    }
}

